package cn.fl.system.service.impl;

import cmm.user.facade.system.api.IPriorityApiService;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.api.ISysUsrQueryApiService;
import cmm.user.facade.system.condition.SysUsrFunAuthRQueryCondition;
import cmm.user.facade.system.dto.SysOrgDTO;
import cmm.user.facade.system.dto.SysRoleDTO;
import cmm.user.facade.system.dto.SysUsrFunAuthRDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.service.ISysUsrFunAuthRService;
import cn.fl.system.transform.SysUsrFunAuthRVOTransform;
import cn.fl.system.vo.SysUsrFunAuthRVO;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUsrFunAuthRServiceImpl implements ISysUsrFunAuthRService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(SysUsrFunAuthRServiceImpl.class);
	
	/**
	 *  权限暴露服务
	 */
	@Autowired
	private IPriorityApiService priorityApiService;

	@Autowired
	private ISysOrgApiService sysOrgApiService;

	@Autowired
	private ISysUsrQueryApiService sysUsrQueryApiService;

	@Override
	public SysUsrFunAuthRVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return null;
		SysUsrFunAuthRDTO dto = null;//this.自定义ApiService.selectById(id);
		return SysUsrFunAuthRVOTransform.toVO(dto);
	}

	@Override
	public boolean addSysUsrFunAuthR(List<SysUsrFunAuthRVO> list) throws BaseException {
		if(CollectionUtils.isEmpty(list)){
			BaseLogUtils.info(logger, "addSysUsrFunAuthR", "新增功能权限关系失败,对象为空..DATA:"+JSONObject.toJSONString(list), CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.priorityApiService.addSysUsrFunAuthR(SysUsrFunAuthRVOTransform.toDTOList(list));
	}

	@Override
	public boolean removeRoleByOrgId(Long orgId, List<Long> roleIds)
			throws BaseException {
		if(StringUtils.isEmpty(orgId)|| CollectionUtils.isEmpty(roleIds)){
			BaseLogUtils.info(logger, "removeRoleByOrgId", "根据组织架构移除关系表的角色信息失败,组织架构ID或角色IDS为空.orgId:"+orgId+",roleIds:"+JSONObject.toJSONString(roleIds), CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.priorityApiService.removeSysUsrFunAuthRRole(orgId, roleIds);
	}

	@Override
	public boolean removeSysUsrFunAuthRUsr(Long orgId, Long roleId,
			List<Long> usrIds) throws BaseException {
		if(StringUtils.isEmpty(orgId)||StringUtils.isEmpty(roleId)||CollectionUtils.isEmpty(usrIds)){
			BaseLogUtils.info(logger, "removeSysUsrFunAuthRUsr", "根据功能权限的关联关系移除功能权限关系表人员信息失败,组织架构ID或角色ID或用户IDS为空.orgId:"+orgId+",roleId:"+roleId+",usrIds:"+JSONObject.toJSONString(usrIds), CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.priorityApiService.removeSysUsrFunAuthRUsr(orgId, roleId, usrIds);
	}

	@Override
	public void setDefaultOrgRole(Long usrId, Long id) {
		priorityApiService.setDefaultFunAuthR(usrId, id);
	}

	@Override
	public List<SysUsrFunAuthRDTO> getUsrSysUsrFunAuthR(Long usrId, String sysCd) throws BaseException {
		if(usrId == null || StringUtils.isEmpty(sysCd)){
			throw new BaseException("用户信息不合法，无法获取角色列表");
		}
		return sysUsrQueryApiService.getUsrSysUsrFunAuthR(usrId, sysCd, null);
	}

	@Override
	public List<SysUsrFunAuthRDTO> searchList(SysUsrFunAuthRQueryCondition qc) throws BaseException {
		List<SysUsrFunAuthRDTO> list = this.priorityApiService.searchSysUsrFunAuthRDTOList(qc);
		if(CollectionUtils.isNotEmpty(list)){
			for(SysUsrFunAuthRDTO sysUsrFunAuthRDTO :list){
				if (null != sysUsrFunAuthRDTO.getRoleId()){
					SysRoleDTO sysRoleDTO = priorityApiService.selectSysRoleById(sysUsrFunAuthRDTO.getRoleId());
					sysUsrFunAuthRDTO.setRoleNm(sysRoleDTO.getRoleNm());
				}
				SysOrgDTO sysOrgDTO = sysOrgApiService.selectById(sysUsrFunAuthRDTO.getOrgId());
				if(sysOrgDTO!=null){
					sysUsrFunAuthRDTO.setOrgDispNm(sysOrgDTO.getOrgDispNm());
				}
			}
		}
		return list;
	}
}
